﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using FSS.Core.Controls;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using FSS.BLL;

namespace SalesManagement.Controls.List
{
    public partial class IndustryForm : BaseControl
    {
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void Show()
        {
            this.winDetails.Show();
        }
        public void Show(string industryId)
        {
            IndustryEntity industryE = new IndustryEntity();
            IndustryService industryS = IndustryService.GetInstance();
            industryE.IndustryId = int.Parse(industryId);
            industryE = industryS.LoadByPrimaryKey(industryE);
            if (null == industryE)
                return;
            hdfIndustryId.Value = industryE.IndustryId;
            txtIndustryName.Text = industryE.IndustryName;
            txtDescription.Text = industryE.Description;
            ckbIsActive.Checked = industryE.IsActive;
            this.winDetails.Show();
        }
        public void ShowAndReset()
        {
            this.FormPanel1.Reset();
            this.winDetails.Show();
        }

        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            IndustryEntity industryE = new IndustryEntity();
            IndustryService industryS = IndustryService.GetInstance();
            if ("" != hdfIndustryId.Value.ToString())
            {
                string industryId = hdfIndustryId.Value.ToString();
                industryE.IndustryId = int.Parse(industryId);
                industryE = industryS.LoadByPrimaryKey(industryE);
                industryE.IndustryName = txtIndustryName.Text;
                industryE.Description = txtDescription.Text;
                industryE.IsActive = ckbIsActive.Checked;
                industryE.UpdatedDate = DateTime.Now;
                industryE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                industryS.Update(industryE);
            }
            else
            {
                industryE.IndustryName = txtIndustryName.Text;
                industryE.Description = txtDescription.Text;
                industryE.IsActive = ckbIsActive.Checked;
                industryE.CreatedDate = DateTime.Now;
                industryE.CreatedBy = FSSContext.Current.User.EmployeeId;
                industryE.UpdatedDate = DateTime.Now;
                industryE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                industryS.Insert(industryE);
            }
            this.winDetails.Hide();
            OnSaveClick();
        }
    }
}